package com.kgc.warehouse.bussness.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kgc.warehouse.bussness.entity.Goods;
import com.kgc.warehouse.bussness.entity.Inport;
import com.kgc.warehouse.bussness.entity.Outport;
import com.kgc.warehouse.bussness.mapper.GoodsMapper;
import com.kgc.warehouse.bussness.mapper.InportMapper;
import com.kgc.warehouse.bussness.mapper.OutportMapper;
import com.kgc.warehouse.bussness.service.GoodsService;
import com.kgc.warehouse.bussness.service.InportService;
import com.kgc.warehouse.bussness.service.OutportService;
import com.kgc.warehouse.system.common.CodeMsg;
import com.kgc.warehouse.system.common.ResponseResult;
import com.kgc.warehouse.system.entity.User;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;

/**
* @author main
* @description 针对表【bus_outport】的数据库操作Service实现
* @createDate 2023-02-04 08:48:32
*/
@Service
@Transactional
public class OutportServiceImpl extends ServiceImpl<OutportMapper, Outport> implements OutportService{

    @Resource
    private GoodsMapper goodsMapper;

    @Resource
    private InportMapper inportMapper;

    @Resource
    private OutportMapper outportMapper;
    @Override
    public ResponseResult addPort(Integer id, Integer number, String remark, HttpSession session) {
        //1.通过进货单ID查询出进货单信息
        Inport inport = inportMapper.selectById(id);

        //2.根据商品ID查询商品信息
        Goods goods = goodsMapper.selectById(inport.getGoodsid());
        System.out.println("goods.getDangernum() = " + goods.getDangernum());
        if (goods.getNumber()-number<goods.getDangernum()&&goods.getNumber()-number>=0){
            return new ResponseResult(CodeMsg.NUMBER_OVER);
        }
        //3.修改商品的数量     商品的数量-退货的数量
        goods.setNumber(goods.getNumber()-number);
        //修改进货的数量
        inport.setNumber(inport.getNumber()-number);
        inportMapper.updateById(inport);
        //修改商品的的数量
        goodsMapper.updateById(goods);

        //添加退货单信息
        Outport outport = new Outport();
        outport.setGoodsid(goods.getId());
        User user = (User) session.getAttribute("user");
        outport.setOperateperson(user.getName());
        outport.setNumber(number);
        outport.setOutportprice(inport.getInportprice());
        outport.setRemark(remark);
        outport.setOutputtime(new Date());
        outport.setPaytype(inport.getPaytype());
        outport.setProviderid(inport.getProviderid());

        outportMapper.insert(outport);
        return new ResponseResult(CodeMsg.BACKINPORT_SUCCESS);
    }

    @Override
    public ResponseResult addPorts(Integer id, Integer number, String remark, HttpSession session) {
        //1.通过进货单ID查询出进货单信息
        Inport inport = inportMapper.selectById(id);

        //2.根据商品ID查询商品信息
        Goods goods = goodsMapper.selectById(inport.getGoodsid());
        System.out.println("goods.getDangernum() = " + goods.getDangernum());
        //3.修改商品的数量     商品的数量-退货的数量
        goods.setNumber(goods.getNumber()-number);
        //修改进货的数量
        inport.setNumber(inport.getNumber()-number);
        inportMapper.updateById(inport);
        //修改商品的的数量
        goodsMapper.updateById(goods);

        //添加退货单信息
        Outport outport = new Outport();
        outport.setGoodsid(goods.getId());
        User user = (User) session.getAttribute("user");
        outport.setOperateperson(user.getName());
        outport.setNumber(number);
        outport.setOutportprice(inport.getInportprice());
        outport.setRemark(remark);
        outport.setOutputtime(new Date());
        outport.setPaytype(inport.getPaytype());
        outport.setProviderid(inport.getProviderid());

        outportMapper.insert(outport);
        return new ResponseResult(CodeMsg.BACKINPORT_SUCCESS);
    }
}




